Lecture 3 - Practice

Q1: Parent & Grandparent Rules

اعمل قاعدة بيانات للعيلة دي:

  1. Aly is the father of Hany.
  2. Saly is the mother of Hany.
  3. Samy is the father of Aly.

وبعدين اعمل الـ rules دي.

واكتب الاستعلامات:

  1. Who is Hany's parent?
  2. Who is Aly's father?
  3. Who is Aly's grandparent?

parent(X, Y) :- mother(X, Y).

grandparent(X, Y) :- parent(X, Z), parent(Z, Y).

**Queries:**
```prolog
?- parent(X, hany).       % X = aly ; X = saly
?- father(Y, aly).         % Y = samy
?- grandparent(X, hany).   % X = samy

Q2: Family Tree Facts

اعمل قاعدة بيانات لشجرة العيلة دي :

  1. Hany is a parent of Samy.
  2. Saly is a parent of Samy.
  3. Saly is a parent of Lila.
  4. Samy is a parent of Aly.
  5. Samy is a parent of Dena.
  6. Dena is a parent of Tamer.
  7. Hany, Samy, Aly, Tamer are male.
  8. Saly, Dena, Lila are female.

Q3: Family Tree Rules

من قاعدة البيانات بتاعة شجرة العيلة (سؤال 2)، اعمل الـ rules دي:

  1. X is the father of Y if X is a parent of Y and X is male.
  2. X is the mother of Y if X is a parent of Y and X is female.
  3. X is a sibling of Y if they both have the same parent Z.
  4. Y is an offspring of X if X is a parent of Y.
  5. X is a sister of Y if X is a sibling of Y and X is female.

Q4: Family Tree Queries

من قاعدة البيانات بتاعة شجرة العيلة (سؤال 2)، اكتب الاستعلامات دي:

  1. Is Samy a parent of Dena?
  2. Is Aly a parent of Dena?
  3. Who is Lila's parent?
  4. Who is a parent of Aly and a parent of Dena?
  5. List all parent relationships.
  6. Who is Tamer's parent?
  7. Who is Dena's child?
  8. Who are the male offspring of Hany?
  9. Who has a sister?
  10. Who is Dena's grandparent?

Q5: Student Age Database

اعمل قاعدة بيانات للطلاب دي using the predicate اstudent(Name, Sex, Age):

Name Sex Age
Ahmed male 30
Hany male 25
Rwan female 20
Mona female 18

وبعدين اعمل rule:

واكتب الاستعلامات:

  1. What is Hany's age?
  2. Who is male?
  3. List names with age < 30 and they are female.
  4. Find students older than Mona.
  5. Find students with age less than Ahmed.
  6. Find students younger than Ahmed and older than Mona.

Q6: son/child Rule

اعمل قاعدة بيانات:

وبعدين اعمل rule:

واكتب الاستعلامات:


Q7: Recursive above (Blocks)1.

اعمل قاعدة بيانات لـ 4 blocks فوق بعضها:
1.1. Block a is on block b.
1.2. Block b is on block c.
1.3. Block c is on block d.

وبعدين اعمل recursive rule:

واكتب الاستعلامات:

  1. Is block b on block c?
  2. Is block a on block d?
  3. Is block a above block d?
  4. What blocks are above block c?

Q8: Recursive predecessor

اعمل قاعدة بيانات:

وبعدين اعمل recursive rule:

واكتب الاستعلامات:

  1. Is a a parent of b?
  2. Is a a predecessor of b?
  3. Is a a predecessor of e?
  4. Who are the predecessors of d?
  5. Who are the descendants of a?

Q9: Recursive is_bigger

اعمل قاعدة بيانات بالحقائق دي:

وبعدين اعمل recursive rule:

واكتب الاستعلامات:

  1. Is an elephant bigger than a monkey?
  2. What is bigger than a donkey?
  3. What is a horse bigger than?
  4. What is bigger than a dog and that thing is also bigger than a monkey?

Q10: Graph Route with Distances

اعمل قاعدة بيانات للـ graph دي using the predicate link(Node1, Node2, Distance):

graph TD

A["a"]

A -->|"2"| B["b"]
A -->|"3"| C["c"]

B -->|"4"| D["d"]
B -->|"5"| E["e"]

C -->|"6"| F["f"]
C -->|"7"| G["g"]

وبعدين اعمل recursive rule:

واكتب الاستعلامات:


Q11: Flights (Faster & Travel)

اعمل قاعدة بيانات للرحلات الجوية دي:

Flight From To Duration (hr)
BA1 Cairo Rome 3.5
AL2 Rome Paris 2.5
BA6 Cairo London 6.2
AF1 Paris Brussels 0.6

وبعدين اعمل rules:

واكتب الاستعلامات:


Q12: Reigns Database

اعمل قاعدة بيانات لفترات حكم الأمراء دي:

Prince Name Reign year To year
Aly 200 220
Samy 221 230
Hany 231 245
Lotfy 246 260

وبعدين اعمل rule: X was a prince during year Y if X reigned between years A and B, and Y is between A and B inclusive.

واكتب الاستعلامات:


Nour Eldeen Mahmoud


Powered by Forestry.md